package extend.client.base;

import extend.client.Client;
import java.util.Map;

/**
 * 请求类
 *<B>说明：</B>
 *<pre>
 * 每个请求对应一个Request 类
 *</pre>
 *<B>示例：</B>
 *<pre>
 *  略
 *</pre>
 *<B>日志：</B>
 *<pre>
 *  略
 *</pre>
 *<B>注意事项：</B>
 *<pre>
 *  略
 *</pre>
 */
public class Request
{
    /**
     * 客户端
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public Client client;

    /**
     * header 信息
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected Header header = null;

    /**
     * 原始内容
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected String content;

    /**
     * 请求参数
     *<B>说明：</B>
     *<pre>
     *  一般是接口api 的参数
     *</pre>
     */
    protected Map<String,Object> data;

    /**
     * 请求对应响应
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected Response response;

    /**
     * 配置参数
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     */
    protected RequestParams requestParams;

    protected String uri;

    protected Transport transport;

    public Request()
    {

    }

    public void setClient(Client client)
    {
        this.client = client;
    }

    public Request(RequestParams requestParams)
    {
        this.requestParams = requestParams;
    }

    public void addHeaders(String name,String value)
    {
        this.getHeader().add(name,value);
    }

    public void setHeader(String name,String value)
    {
        this.getHeader().set(name,value);
    }

    public Header getHeaders()
    {
        return this.header;
    }

    public Header getHeader()
    {
        if (this.header == null) {
            this.header = new Header(this.requestParams.getHeader());
        }

        return this.header;
    }

    public boolean hasHeader()
    {
        if (this.header == null) {
            return false;
        } else {
            return true;
        }
    }

    public void setContent(String content)
    {
        this.content = content;
    }

    public String getContent()
    {
        return this.content;
    }

    public void setData(Map<String,Object> data)
    {
        this.data = data;
    }

    public Map<String,Object> getData()
    {
        return this.data;
    }

    public void setResponse(Response response)
    {
        this.response = response;
    }

    public Response getResponse()
    {
        return this.response;
    }

    public RequestParams getRequestParams()
    {
        return this.requestParams;
    }

    public void setRequestParams(RequestParams requestParams)
    {
        this.requestParams = requestParams;
    }

    public void setUri(String uri)
    {
        this.uri = uri;
    }

    public String getUri()
    {
        return this.uri;
    }

    public Transport getTransport()
    {
        if (this.transport == null) {
            this.transport = this.requestParams.createTransport();
        }

        return this.transport;
    }

    /**
     * 请求之前准备数据
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     */
    public void prepare()
    {
        String content = this.getContent();
        if (content == null) {
            this.requestParams.createFormatter().format(this);
        }
    }

    /**
     * 头部信息
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     */
    private String composeHeaderLines()
    {
        return "";
    }

    public Response send()
    {
        // 创建协议
        this.getTransport().send(this);
        return this.getResponse();
    }


}
